package com.houdoudou.customer;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import com.houdoudou.bean.Customer;
import com.houdoudou.customer.common.Constant;
import com.houdoudou.customer.common.RoundImageView;
import com.houdoudou.customer.common.UniversalHttpPost;
import com.houdoudou.customer.me.LoginAct;
import com.houdoudou.customer.me.NetConfigActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FrgMeOfActMain extends Fragment{
  
  

Button netconfig;
   
   RoundImageView  customerhead;
   TextView customertel;
   View.OnClickListener myOnClickListener;
  public  boolean isLogined=false;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		View view=inflater.inflate(R.layout.frg_me_act_main, container, false);
	      init_listener();
	      init_component(view);
	      
	    
		return view;
	}
	 @Override
		public void onResume() {
			// TODO Auto-generated method stub
		  if(Constant.customerJO==null){
	    	  customertel.setText("尚未登录，请登录");
	    	  SharedPreferences sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
	    	  String customerStr=sp.getString("customer", "");
		       if(!customerStr.isEmpty()){
		    	   JSONObject customerJO=JSONObject.fromObject(customerStr); 
		    		String[] params= new String[3];
		            params[0]=Constant.BaseUrl+Constant.CustomerUrl;
		            params[1]="login"; //PostCod
		            JSONObject jsonData=new JSONObject();			    		  		        
		            jsonData.put(Customer.TELNUM, customerJO.getString(Customer.TELNUM));	
		        	jsonData.put(Customer.SECRET, customerJO.getString(Customer.SECRET));
		        	params[2]=jsonData.toString();
		            new HttpPostTaskforAutoLogin().execute(params);
	      }} else{ 
	    	  customertel.setText(Constant.customerJO.getString(Customer.TELNUM));    }
	
			super.onResume();
		}
	private void init_component(View view){
		
		  netconfig=(Button) view.findViewById(R.id.btn_netconfig_frgme_mainact);
		  customertel= (TextView) view.findViewById(R.id.tv_customertel_frgme_mainact);
		  customerhead= (RoundImageView) view.findViewById(R.id.rndimg_head_frgme_mainact);
		  
	      netconfig.setOnClickListener(myOnClickListener);
	      customerhead.setOnClickListener(myOnClickListener);
	      
	    
	}
	
	private void init_listener(){
		myOnClickListener= new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(v==netconfig){
					Intent intent= new Intent(getActivity(), NetConfigActivity.class);    	   
				     startActivity(intent);			
				                }
				if(v==customerhead){
					if(!isLogined){
					Intent intent= new Intent(getActivity(), LoginAct.class);    	   
				     startActivity(intent);	}
				                }
				
			}
		};
	}
	  private  class HttpPostTaskforAutoLogin extends android.os.AsyncTask<String,String,Map<String,Object>>{

			

			@Override
			protected Map<String,Object> doInBackground(String... params) {
				// TODO Auto-generated method stub
				 Map<String,Object> map = new HashMap<String ,Object>();		
			      map =	UniversalHttpPost.doPost(params);	             
				return map;
			}
			
			@Override
			protected void onPostExecute(Map<String,Object> map) {
			
				 System.out.println("----------- Customer AUTO Login onPostExecute()--------------");
				 String PostResult=(String) map.get("PostResult");		 
				 boolean networkstate=(Boolean) map.get("PostState");
				 if(!networkstate){
					return ;   
				        }
				//	Toast.makeText(getActivity(), "服务器返回数据为"+PostResult, Toast.LENGTH_SHORT).show();
					Boolean success=false;
					String errorInfo="";
					JSONObject json = null;
					
					try{
				     json= JSONObject.fromObject(PostResult);
					 success=json.getBoolean("success");
					 errorInfo=json.getString("errorInfo");	
					}catch(JSONException e){
						return;
					}
											   												      		       
			       if(success){     
			    	   Constant.customerJO=json.getJSONObject("customer");
			    	   customertel.setText(Constant.customerJO.getString(Customer.TELNUM));
			    	  
			    		 SharedPreferences sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
			   		     Editor editor = sp.edit();  
			   		     editor.putString("customer", Constant.customerJO.toString());
			   		     editor.commit();
		             }
					
				
			} //onPostExecute
		} //AsyncTask
}
